/**
 * AddressDAO.java
 *
 * Enterprise Java
 *
 * Author:  Matthew Braddy
 * Created: Nov 21, 2010
 *
 */

package java113.labs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import java113.dataAccess.DAOBase;
import java113.dataAccess.DataManager;

/**
 *
 * @author Matthew Braddy
 */
public class AddressDAO extends DAOBase {

    private Properties properties;

    private DataManager m_oDataManager;

    private Address address;

    public AddressDAO(Properties properties, DataManager m_oDataManager) throws Exception {

        this.properties = properties;

        this.m_oDataManager = m_oDataManager;

        String sqlFileName = this.properties.getProperty("path.sql") +
                             this.properties.getProperty("sql.file.address");

        this.load(sqlFileName, this.properties.getProperty("webApp"));

    }

    public Address getAddress(int inId) throws SQLException {

        String strSQL;
        ResultSet resultSet = null;
        Statement statement = null;

        try
        {
            strSQL = this.getSQL ("get address");  //obtain the SQL by name that is loaded in memory

            strSQL = strSQL.replace ("%%ID%%", String.valueOf (inId));

            statement = (Statement) this.m_oDataManager.createStatement ();  //create a statement for our pending transaction
            resultSet = this.m_oDataManager.executeQuery ((java.sql.Statement) statement,strSQL);  //execute the query

            if(resultSet != null)  //defensive coding
            {

                while (resultSet.next()) {

                    address = new Address();

                    int    id     = resultSet.getInt("id");
                    String street = resultSet.getString("street");
                    String city   = resultSet.getString("city");
                    String state  = resultSet.getString("state");
                    String zip    = resultSet.getString("zip");

                    address.setId(id);
                    address.setStreet(street);
                    address.setCity(city);
                    address.setState(state);
                    address.setZip(zip);

                }

            }

            this.m_oDataManager.close ((java.sql.Statement) statement);
        }
        catch(Exception Ex)
        {
            Ex.printStackTrace ();
        }

        return address;

    }

    public void setAddress(int inId, String inStreet, String inCity, String inState, String inZip) throws SQLException {

        String strSQL;
        ResultSet resultSet = null;
        Statement statement = null;

        try
        {
            strSQL = this.getSQL ("set address");

            strSQL = strSQL.replace ("%%ID%%", String.valueOf (inId));
            strSQL = strSQL.replace ("%%STREET%%", String.valueOf (inStreet));
            strSQL = strSQL.replace ("%%CITY%%", String.valueOf (inCity));
            strSQL = strSQL.replace ("%%STATE%%", String.valueOf (inState));
            strSQL = strSQL.replace ("%%ZIP%%", String.valueOf (inZip));

            statement = this.m_oDataManager.createStatement ();  //create a statement for our pending transaction
            resultSet = this.m_oDataManager.executeUpdate ((java.sql.Statement) statement,strSQL);  //execute the query

            this.m_oDataManager.close ((java.sql.Statement) statement);
            
        }
        catch(Exception Ex)
        {
            Ex.printStackTrace ();
        }

    }

}
